﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DarkBO.Helpers;
using System.Data;

namespace DarkBO
{
    public class Production
    {
        public string Title { get; set; }
        public string Message { get; set; }
        public DateTime CreateDate { get; set; }
        public bool IsDelete { get; set; }
        public int ID { get; set; }
        public Production()
        {

        }

        [DarkAjax.Utility.jQueryInvokeMethod(Url = "handlers/AjaxController.ashx", Method = "post", DataType = "json", Async = true)]
        public static ResultMessage Create(Production production)
        {
            string connectionString = GenHelper.GenConnectionString();

            string commond = string.Format("insert into production (title,message,createdate,isdelete) values ('{0}','{1}','{2}',{3})", production.Title, production.Message, DateTime.Now, 0);
            try
            {
                AccessHelper.ExecuteScalar(connectionString, commond);
                return new ResultMessage() { ErrorCode = 0 };

            }
            catch (Exception ex)
            {
                return new ResultMessage() { ErrorCode = 1, Description = ex.Message + "." + commond };
            }
        }

        [DarkAjax.Utility.jQueryInvokeMethod(Url = "handlers/AjaxController.ashx", Method = "post", DataType = "json", Async = false)]
        public static ResultMessage GetDetail(int ID)
        {
            DataTable dt = Helpers.AccessHelper.ExecuteDataset(GenHelper.GenConnectionString(), "select * from production where id=" + ID.ToString()).Tables[0];
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                int id = int.Parse(dr["id"].ToString());
                string title = dr["title"].ToString();
                string message = dr["message"].ToString();
                DateTime createdate = DateTime.Parse(dr["createdate"].ToString());
                return new ResultMessage()
                {
                    ErrorCode = 1,
                    Value = new Production()
                        {
                            ID = id,
                            Title = title,
                            Message = message,
                            CreateDate = createdate
                        }
                };
            }
            else
            {
                return new ResultMessage() { ErrorCode = 0, Description = "没有数据" };
            }


        }
        [DarkAjax.Utility.jQueryInvokeMethod(Url = "handlers/AjaxController.ashx", Method = "post", DataType = "json", Async = false)]
        public static ResultMessage GetPageing(int pagesize, int pagenum)
        {
            HttpRequest request = HttpContext.Current.Request;
            if (request["pagesize"] != null)
                pagesize = int.Parse(request["pagesize"]);
            if (request["pagenum"] != null)
                pagenum = int.Parse(request["pagenum"]);

            var admins = GetAll();
            var ps = admins.Select(m => new { Title = m.Title, Message = m.Message, CreateDate = m.CreateDate, ID = m.ID }).Skip((pagenum - 1) * pagesize).Take(pagesize); ;
            //var ps = from p in GetAllProductions() select new { p.Title,p.Description,p.Category,p.SubCategory};  //new { title="001",content="hellaaaaaaaaaaaaaao",category=1,subcategory=2 };
            var data = new { total = admins.Count, rows = ps };
            return new ResultMessage() { ErrorCode = 0, Name = "success", Value = data };
        }
        [DarkAjax.Utility.jQueryInvokeMethod(Url = "handlers/AjaxController.ashx", Method = "post", DataType = "json", Async = true)]
        public static ResultMessage Delete(string id)
        {
            string connectionString = GenHelper.GenConnectionString();
            string commond = string.Format("update production set isdelete=1 where id={0}", id);
            Helpers.AccessHelper.ExecuteScalar(connectionString, commond);
            return new ResultMessage() { ErrorCode = 0 };
        }
        public static IList<Production> GetAll()
        {
            IList<Production> list = new List<Production>();
            DataTable dt = Helpers.AccessHelper.ExecuteDataset(GenHelper.GenConnectionString(), "select * from production where isdelete=0").Tables[0];
            foreach (DataRow dr in dt.Rows)
            {
                int id = int.Parse(dr["id"].ToString());
                string title = dr["title"].ToString();
                string message = dr["message"].ToString();
                DateTime createdate = DateTime.Parse(dr["createdate"].ToString());
                list.Add(new Production()
                {
                    ID = id,
                    Title = title,
                    Message = message,
                    CreateDate = createdate
                });
            }
            return list;
        }


        [DarkAjax.Utility.jQueryInvokeMethod(Url = "handlers/AjaxController.ashx", Method = "post", DataType = "json", Async = true)]
        public static ResultMessage Update(Production production)
        {
            string connectionString = GenHelper.GenConnectionString();
            string commond = string.Format("update production set title='{0}',message='{1}' where id={2}",production.Title,production.Message,production.ID);
            Helpers.AccessHelper.ExecuteScalar(connectionString, commond);
            return new ResultMessage() { ErrorCode = 0 };
        }
    }
}